<?php

function success($result){
    global $err;
    $r = array('err'=>$err, "success"=>true, 'data'=>$result);

    echo json_encode($r, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    die();
}

function fail($errr){
    global $err;
    $r = array('err'=>$err, "success"=>false, 'data'=>$errr);
    echo json_encode($r, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    die();
}

function error($error){
    global $err;
    $err .= $error.'
    ';
    return false;
}

// 检查传入参数是否为空
function checkParamEmpty($arr)
{
    if (is_array($arr)) {
        $post = I('post.');
        foreach ($arr as $param) {
            if (empty($post[$param]))
            fail(L('post_' . $param . '_blank')); // return $param;
        }
        // return false;
    } elseif (is_string($arr)) {
        if (empty(I('post.' . $arr)))
        fail(L('post_' . $arr . '_blank')); // $arr;
                                                                // return false;
    }
    // return true;
}

function getRedis(){
    $redis = new \Redis() or die('load redis error');
    $redis->connect('127.0.0.1');
    return $redis;
}

function cfg($name, $field = false){
    // $redis = getRedis();
    // return json_decode($redis->hGet('config', $name), true);
    $json = file_get_contents('Public/'.$name.'.json');
    $json = json_decode($json, true);
    if($field){
        return $json[$field];
    }
    return $json;
}

function getPre(){
    $num = func_num_args();
    $list = func_get_args();
    $pre = $list[0];
    for($i=1; $i<$num; $i++){
        $pre = $pre.'_'.$list[$i];
    }
    return $pre.':';
}

function getClass($name, $user, $arg = null){
    $class = '\\Home\\Model\\'.ucfirst($name);
    if(!$arg){
        return new $class($user);
    }else{
        return new $class($user, $arg);
    }
}